package com.xczz.controller;

import com.xczz.common.base.BaseController;
import com.xczz.common.constant.UserConstants;
import com.xczz.common.result.RestResult;
import com.xczz.model.SysRole;
import com.xczz.model.vo.SysRoleVO;
import com.xczz.model.vo.SysUserVO;
import com.xczz.service.SysRoleService;
import com.xczz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/roles")
public class SysRoleController extends BaseController {

    @Autowired
    public SysRoleService roleService;
    @PostMapping
    public RestResult add(@Validated @RequestBody SysRoleVO role){
//        if(UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))){
//            return RestResult.error("新增角色'"+role.getRoleName()+"'失败，角色名称已存在");
//        } else if(UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))){
//            return RestResult.error("新增角色'"+role.getRoleName()+"'失败，角色权限已存在");
//        }
        role.setCreateBy("system");
        return toAjax(roleService.insertRole(role));
    }

    @PutMapping
    public RestResult edit(@Validated @RequestBody SysRoleVO role){
        role.setUpdateBy("system");
        boolean flag=roleService.updateRole(role);
        return toAjax(flag?1:0);
    }

    @DeleteMapping("/{roleIds}")
    public RestResult remove(@PathVariable Long[] roleIds){
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }

    @GetMapping(value = {"/", "/{roleId}"})
    public RestResult getRoleInfo(
            @PathVariable(value = "roleId", required = false) Long roleId) {

        RestResult result = RestResult.success();
//
//        // 1. 总是返回所有角色列表
//        result.put("roles", roleService.selectRoleAll());

        // 2. 如果传了roleId，额外返回该角色详情
        if (StringUtils.isNotNull(roleId)) {
            result.put(RestResult.DATA_TAG, roleService.getRoleById(roleId));
        }

        return result;
    }
}
